﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MixingStation
{
    public partial class SupplierForm : Form
    {
        private bool m_IsNew = true;
        public bool IsNew
        {
            get { return m_IsNew; }
            set { m_IsNew = value; }
        }

        private string m_Code = "";
        public string Code
        {
            get { return m_Code; }
            set { m_Code = value; }
        }

        private string m_SupplierName = "";
        public string SupplierName
        {
            get { return m_SupplierName; }
            set { m_SupplierName = value; }
        }
        public SupplierForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                MessageBox.Show("请输入供应商编码！");
                return;
            }
            if (string.IsNullOrEmpty(txtSupplier.Text.Trim()))
            {
                MessageBox.Show("请输入供应商名称！");
                return;
            }
            m_Code = this.txtCode.Text.Trim();
            m_SupplierName = this.txtSupplier.Text.Trim();

            string sqlStr = "";
            DataTable dt = new DataTable ();
            if (m_IsNew)
            {
                //判断是否存在相同的编码
                sqlStr = "select * from Supplier where Code = '" + m_Code + "'";
                dt = CommonHelper.DoSelectOpreation(sqlStr);
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("已存在相同的供应商编码");
                    return;
                }
            }
            //判断是否存在相同的供应商
            if(m_IsNew)
                sqlStr = "select * from Supplier where Name = '" + m_SupplierName + "'";
            else
                sqlStr = "select * from Supplier where Name = '" + m_SupplierName + "' and Code <> '" + m_Code + "'";
            dt = CommonHelper.DoSelectOpreation(sqlStr);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("已存在相同的供应商名称");
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void SupplierForm_Load(object sender, EventArgs e)
        {
            if (m_IsNew)
            {
                txtCode.Text = "";
                txtCode.Enabled = true;
                txtSupplier.Text = "";
                this.Text = "添加供应商";
            }
            else
            {
                this.txtCode.Text = m_Code;
                txtCode.Enabled = false;
                this.txtSupplier.Text = m_SupplierName;
                this.Text = "修改供应商";
            }
        }
    }
}
